package com.ziqing.apeletnews.activity;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageView;

import com.ziqing.apeletnews.R;
import com.ziqing.apeletnews.base.BaseActivity;
import com.ziqing.apeletnews.baseMvp.BasePresenter;
import com.ziqing.apeletnews.fragment.RemindFragment;
import com.ziqing.apeletnews.util.TablayoutLineWidth;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ReMindActivity extends BaseActivity {

    @BindView(R.id.iv_back)
    ImageView iv_back;
    @BindView(R.id.tl_tab)
    TabLayout tablayout;
    @BindView(R.id.viewpager)
    ViewPager home_view_pager;
    private List<Fragment> fragments = new ArrayList<>();
    private List<String> data = new ArrayList<>();
    List<TabLayout.Tab> tabs = new ArrayList<>();
    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_re_mind);
        ButterKnife.bind(this);
        home_view_pager.setOffscreenPageLimit(0);
        data.add("通知");
        data.add("消息");
        TablayoutLineWidth.reflex(this,tablayout,30);
        initView();

    }


    @OnClick(R.id.iv_back)
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.iv_back) {
            finish();
        }
    }

    @Override
    protected void initView() {
        for (int i = 0;i<data.size();i++){
            fragments.add(new RemindFragment(data.get(i)));
            tabs.add(tablayout.getTabAt(i));
        }
        FragmentPagerAdapter ps = new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return fragments.get(position);
            }
            @Override
            public int getCount() {
                return fragments.size();
            }

            @Override
            public CharSequence getPageTitle(int position) {
                return data.get(position);
            }
        };
        tablayout.setupWithViewPager(home_view_pager);
        home_view_pager.setOffscreenPageLimit(fragments.size());
        home_view_pager.setAdapter(ps);
        home_view_pager.setOffscreenPageLimit(data.size());
        tablayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                for (int i = 0;i<fragments.size();i++){
                    if(tab == tablayout.getTabAt(i)){
                        home_view_pager.setCurrentItem(i,false);
                    }
                }
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }
    }

